﻿package game.network.api {
	import game.Config;
	import game.objects.GameObject;
	import game.network.INetwork;
	import game.objects.UserApiObject;
	import game.network.api.fb.Facebook;
	import game.events.FBLocalEvent;
	
	public class fbPlugin extends GameObject implements INetwork {
		
		public static const FIELDS:String				= "id,first_name,last_name,username,link,gender,picture.type(large)";
		
		public static const GET_FRIENDLIST:String		= "me/friendlists";
		public static const GET_ME:String				= "me/";
		public static const GET_PROFILES:String			= "";
		
		
		public function fbPlugin():void {
			
			Facebook.init(Config.FB_APP_ID, onInit);
			
		}
		
		private function onInit (result:Object, fail:Object):void { 
			if (result)
				this.dispatchEvent( new FBLocalEvent ( FBLocalEvent.CONN_INIT ) );
			else
				this.dispatchEvent( new FBLocalEvent ( FBLocalEvent.CONN_ERROR ) );
		}
		
		public function getProfiles(uids:String, callBack:Function):void {
			if (uids==null) {
				getMe(callBack);
				return;
			}
		}
		
		private function getMe(callBack:Function):void {
			Facebook.api(fbPlugin.GET_ME, function (obj:Object) {
				var v:Vector.<UserApiObject> = new Vector.<UserApiObject>;
				v.push( new UserApiObject ( obj.id, obj.first_name, obj.last_name, obj.username, obj.picture.data.url, genderToSex(obj.gender), obj.link ) );
				callBack(v);
			}, {fields: fbPlugin.FIELDS});
		}
		
		public function getFriends(callBack:Function):void {
			
		}
		
		private function genderToSex(value:String):int {
			var sex:int = 0;
			if (value=='male') sex = 2;
			else if (value=='female') sex = 1;
			else sex = 0;
			return sex;
		}
		
	}
	
}